package com.briup.MR.workflow;

import com.briup.MR.Combiner.SumValue;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/*
读入
TextInputFormat
结果落地
SequecneFileOutputFormat
 */
public class FirstAvgTempMR {
    static class FirstAvgTempMRMapper extends Mapper<LongWritable, Text,Text, SumValue>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] strs=value.toString().split(",");
            SumValue v=
                    new SumValue(Double.parseDouble(strs[1]),
                            1);
            context.write(new Text(strs[0]),v);
        }
    }
    static class FirstAvgTempMRReduce
            extends Reducer<Text,SumValue,Text,SumValue> {
        @Override
        protected void reduce(Text key, Iterable<SumValue> values, Context context) throws IOException, InterruptedException {
            int nums=0;
            double sum_temp=0;
            for(SumValue val:values){
                nums+=val.getNum();
                sum_temp+=val.getTemp();
            }
            context.write(key,new SumValue(sum_temp,nums));
        }
    }
}
